package com.jwdf.uda.ui.fragment.find;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.ItemShowMultiImageBinding;
import com.jwdf.uda.databinding.ItemShowSingleImageBinding;
import com.jwdf.uda.databinding.ItemShowTextBinding;
import com.jwdf.uda.ui.activity.PhotoActivity;
import com.jwdf.uda.ui.adapter.AbsAdapter;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.utils.ConfigUtils;
import com.jwdf.uda.utils.DateUtils;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.GridImageLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/18.
 */
public class UserShowAdapter extends AbsAdapter<PbBean.PbHomeRecommend> {
    private static final int ITEM_TEXT = 0;
    private static final int ITEM_SINGLE_IMAGE = 1;
    private static final int ITEM_MULTI_IMAGE = 2;
    private OnUserShowItemClickListener mListener;
    private OnUserInfoClickListener mInfoListener;

    public UserShowAdapter(Context context, OnUserShowItemClickListener listener, OnUserInfoClickListener l) {
        this(context);
        this.mListener = listener;
        this.mInfoListener = l;
    }

    public UserShowAdapter(Context context) {
        super(context);
    }
    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ITEM_SINGLE_IMAGE:
                return new SingleImageHolder(mInflater.inflate(R.layout.item_show_single_image, parent, false));
            case ITEM_MULTI_IMAGE:
                return new MultiImageHolder(mInflater.inflate(R.layout.item_show_multi_image, parent, false));
            default:
                return new TextHolder(mInflater.inflate(R.layout.item_show_text, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        PbBean.PbHomeRecommend bean = getItem(position);
//        if (bean.getImageListList() == null || bean.getImageListCount() == 0) {
//            return ITEM_TEXT;
//        } else if (bean.getImageListCount() == 1) {
//            return ITEM_SINGLE_IMAGE;
//        } else {
//            return ITEM_MULTI_IMAGE;
//        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    private void onZanClick(Context context, PbBean.PbHomeRecommend item) {
        boolean isOwner = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(context));
//        PersonalShowDetailActivity.start(context, item.getId(), isOwner, 1);
    }

    public interface OnUserShowItemClickListener {
        void onItemClick(int showId, boolean isOwner);
    }

    public interface OnUserInfoClickListener {
        void onInfoClick(String userId, boolean isDesigner);
    }
    private class TextHolder extends AbsViewHolder<PbBean.PbHomeRecommend> {
        private ItemShowTextBinding mBinding;

        public TextHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            convertView.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
            mBinding.topLayout.tvName.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final PbBean.PbHomeRecommend item) {
            mBinding.topLayout.tvDate.setText(DateUtils.getFriendlyTime(item.getCreateTime()));
            mBinding.topLayout.tvName.setText(item.getUserInfo().getNickname());
            mBinding.setIsDesigner(item.getUserInfo().getIsDesigner());
//            mBinding.topLayout.tvContent.setText(item.getContent());
            if (StringUtils.isNullEmpty(item.getContent())) {
                mBinding.topLayout.tvContent.setVisibility(View.GONE);
            } else {
                mBinding.topLayout.tvContent.setVisibility(View.VISIBLE);
                mBinding.topLayout.tvContent.setText(item.getContent());
            }
//            if (item.getStyleTagListCount() > 0) {
//                mBinding.topLayout.tvStyle.setText(mBinding.getRoot().getContext().getString(R.string.style_tag_format, item.getStyleTagList(0)));
//            } else {
//                mBinding.topLayout.tvStyle.setVisibility(View.GONE);
//            }
            ImageLoader.getInstance().displayImage(item.getUserInfo().getAvatar().getUrl(), mBinding.topLayout.ivHeader);
            mBinding.bottomLayout.tvZanCount.setText("" + item.getLikeCount());
            mBinding.bottomLayout.tvCommentCount.setText("" + item.getCommentCount());
            mBinding.bottomLayout.llZanCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onZanClick(v.getContext(), item);
                }
            });
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.iv_header || v.getId() == R.id.tvName) {
                if (mInfoListener != null) {
                    mInfoListener.onInfoClick(item.getUserInfo().getUserId(), item.getUserInfo().getIsDesigner());
                }
                return;
            }
            if (mListener != null) {
                boolean isOwner = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(v.getContext()));
//                mListener.onItemClick(item.getId(), isOwner);
            }
        }

    }
    private class SingleImageHolder extends AbsViewHolder<PbBean.PbHomeRecommend> {
        private ItemShowSingleImageBinding mBinding;

        public SingleImageHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            convertView.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
            mBinding.topLayout.tvName.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final PbBean.PbHomeRecommend item) {
            mBinding.topLayout.tvName.setText(item.getUserInfo().getNickname());
            mBinding.topLayout.tvDate.setText(DateUtils.getFriendlyTime(item.getCreateTime()));
            mBinding.setIsDesigner(item.getUserInfo().getIsDesigner());
//            mBinding.topLayout.tvContent.setText(item.getContent());
            if (StringUtils.isNullEmpty(item.getContent())) {
                mBinding.topLayout.tvContent.setVisibility(View.GONE);
            } else {
                mBinding.topLayout.tvContent.setVisibility(View.VISIBLE);
                mBinding.topLayout.tvContent.setText(item.getContent());
            }
//            if (item.getStyleTagListCount() > 0) {
//                mBinding.topLayout.tvStyle.setText(mBinding.getRoot().getContext().getString(R.string.style_tag_format, item.getStyleTagList(0)));
//            } else {
//                mBinding.topLayout.tvStyle.setVisibility(View.GONE);
//            }
            ImageLoader.getInstance().displayImage(item.getUserInfo().getAvatar().getUrl(), mBinding.topLayout.ivHeader);
            mBinding.bottomLayout.tvZanCount.setText("" + item.getLikeCount());
            mBinding.bottomLayout.tvCommentCount.setText("" + item.getCommentCount());
            mBinding.bottomLayout.llZanCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onZanClick(v.getContext(), item);
                }
            });

//            final List<PbBean.PbImage> images = item.getImageListList();
            final ArrayList<String> urls = new ArrayList<String>();
//            for (PbBean.PbImage image : images) {
//                urls.add(image.getUrl());
//            }
            if (urls.size() > 0) {
                ImageLoader.getInstance().displayImage(urls.get(0), mBinding.image);
            }
            mBinding.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PhotoActivity.start(v.getContext(), urls, 0);
                }
            });
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.iv_header || v.getId() == R.id.tvName) {
                if (mInfoListener != null) {
                    mInfoListener.onInfoClick(item.getUserInfo().getUserId(), item.getUserInfo().getIsDesigner());
                }
                return;
            }
            if (mListener != null) {
                boolean isOwner = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(v.getContext()));
//                mListener.onItemClick(item.getId(), isOwner);
            }
        }

    }
    private class MultiImageHolder extends AbsViewHolder<PbBean.PbHomeRecommend> {
        private ItemShowMultiImageBinding mBinding;

        public MultiImageHolder(View convertView) {
            super(convertView);
            mBinding = DataBindingUtil.bind(convertView);
            convertView.setOnClickListener(this);
            mBinding.topLayout.ivHeader.setOnClickListener(this);
            mBinding.topLayout.tvName.setOnClickListener(this);
        }

        @Override
        protected void onBindView(final PbBean.PbHomeRecommend item) {
            mBinding.setIsDesigner(item.getUserInfo().getIsDesigner());
            ImageLoader.getInstance().displayImage(item.getUserInfo().getAvatar().getUrl(), mBinding.topLayout.ivHeader);
            mBinding.topLayout.tvDate.setText(DateUtils.getFriendlyTime(item.getCreateTime()));
            mBinding.bottomLayout.tvZanCount.setText("" + item.getLikeCount());
            mBinding.bottomLayout.tvCommentCount.setText("" + item.getCommentCount());
            mBinding.bottomLayout.llZanCount.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onZanClick(v.getContext(), item);
                }
            });
            mBinding.topLayout.tvName.setText(item.getUserInfo().getNickname());
//            mBinding.topLayout.tvContent.setText(item.getContent());
            if (StringUtils.isNullEmpty(item.getContent())) {
                mBinding.topLayout.tvContent.setVisibility(View.GONE);
            } else {
                mBinding.topLayout.tvContent.setVisibility(View.VISIBLE);
                mBinding.topLayout.tvContent.setText(item.getContent());
            }
//            if (item.getStyleTagListCount() > 0) {
//                mBinding.topLayout.tvStyle.setText(mBinding.getRoot().getContext().getString(R.string.style_tag_format, item.getStyleTagList(0)));
//            } else {
//                mBinding.topLayout.tvStyle.setVisibility(View.GONE);
//            }
//            final List<PbBean.PbImage> images = item.getImageListList();
            final ArrayList<String> urls = new ArrayList<String>();
//            for (PbBean.PbImage image : images) {
//                urls.add(image.getUrl());
//            }
            mBinding.gridimageLayout.setImage(urls);
            mBinding.gridimageLayout.setOnImageItemClickListener(new GridImageLayout.OnImageItemClickListener() {
                @Override
                public void onImageClick(int index, ImageView imageView) {
                    PhotoActivity.start(imageView.getContext(), urls, index);
                }
            });
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.iv_header || v.getId() == R.id.tvName) {
                if (mInfoListener != null) {
                    mInfoListener.onInfoClick(item.getUserInfo().getUserId(), item.getUserInfo().getIsDesigner());
                }
                return;
            }
            if (mListener != null) {
                boolean isOwner = TextUtils.equals(item.getUserInfo().getUserId(), ConfigUtils.getUserId(v.getContext()));
//                mListener.onItemClick(item.getId(), isOwner);
            }
        }

    }
}
